/* -*- Mode: C; c-basic-offset: 4 -*-
 * pygtk- Python bindings for the GTK toolkit.
 * Copyright (C) 1998-2003  James Henstridge
 *
 *   gdkrectangle.override: gtk.gdk.Rectangle overrides
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
 * USA
 */
%%
override gdk_rectangle_new kwargs
static int
_wrap_gdk_rectangle_new(PyGBoxed *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "x", "y", "width", "height", NULL };
    GdkRectangle rect = {0, 0, 0, 0};

    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
				     "|iiii:GdkRectangle.__init__",
				     kwlist, &(rect.x), &(rect.y),
				     &(rect.width), &(rect.height)))
	return -1;

    self->boxed =  g_boxed_copy(GDK_TYPE_RECTANGLE, &rect);
    self->free_on_dealloc = TRUE;
    self->gtype = GDK_TYPE_RECTANGLE;
    
    return 0;
}
%%
override-slot GdkRectangle.tp_as_sequence
static Py_ssize_t
_wrap_gdk_rectangle_length(PyGBoxed *self)
{
    return 4;
}
static PyObject *
_wrap_gdk_rectangle_getitem(PyGBoxed *self, Py_ssize_t pos)
{
    GdkRectangle *rect;

    if (pos < 0) pos += 4;
    if (pos < 0 || pos >= 4) {
        PyErr_SetString(PyExc_IndexError, "index out of range");
        return NULL;
    }
    rect = pyg_boxed_get(self, GdkRectangle);
    switch (pos) {
    case 0: return PyInt_FromLong(rect->x);
    case 1: return PyInt_FromLong(rect->y);
    case 2: return PyInt_FromLong(rect->width);
    case 3: return PyInt_FromLong(rect->height);
    default:
        g_assert_not_reached();
        return NULL;
    }
}
static int
_wrap_gdk_rectangle_setitem(PyGBoxed *self, Py_ssize_t pos, PyObject *value)
{
    GdkRectangle *rect;
    gint val;

    if (pos < 0) pos += 4;
    if (pos < 0 || pos >= 4) {
        PyErr_SetString(PyExc_IndexError, "index out of range");
        return -1;
    }
    rect = pyg_boxed_get(self, GdkRectangle);
    val = PyInt_AsLong(value);
    if (PyErr_Occurred())
        return -1;
    switch(pos) {
    case 0: rect->x      = val; break;
    case 1: rect->y      = val; break;
    case 2: rect->width  = val; break;
    case 3: rect->height = val; break;
    default:
        g_assert_not_reached();
        return -1;
    }
    return 0;
}
static PySequenceMethods _wrap_gdk_rectangle_tp_as_sequence = {
    (lenfunc)_wrap_gdk_rectangle_length,
    0,
    0,
    (ssizeargfunc)_wrap_gdk_rectangle_getitem,
    0,
    (ssizeobjargproc)_wrap_gdk_rectangle_setitem,
    0,
};
%%
override-attr GdkRectangle.x
static int
_wrap_gdk_rectangle__set_x(PyGBoxed *self, PyObject *value, void *closure)
{
    gint val;

    val = PyInt_AsLong(value);
    if (PyErr_Occurred())
        return -1;
    pyg_boxed_get(self, GdkRectangle)->x = val;
    return 0;
}
%%
override-attr GdkRectangle.y
static int
_wrap_gdk_rectangle__set_y(PyGBoxed *self, PyObject *value, void *closure)
{
    gint val;

    val = PyInt_AsLong(value);
    if (PyErr_Occurred())
        return -1;
    pyg_boxed_get(self, GdkRectangle)->y = val;
    return 0;
}
%%
override-attr GdkRectangle.width
static int
_wrap_gdk_rectangle__set_width(PyGBoxed *self, PyObject *value, void *closure)
{
    gint val;

    val = PyInt_AsLong(value);
    if (PyErr_Occurred())
        return -1;
    pyg_boxed_get(self, GdkRectangle)->width = val;
    return 0;
}
%%
override-attr GdkRectangle.height
static int
_wrap_gdk_rectangle__set_height(PyGBoxed *self, PyObject *value, void *closure)
{
    gint val;

    val = PyInt_AsLong(value);
    if (PyErr_Occurred())
        return -1;
    pyg_boxed_get(self, GdkRectangle)->height = val;
    return 0;
}
%%
override gdk_rectangle_intersect kwargs
static PyObject *
_wrap_gdk_rectangle_intersect(PyGObject *self, PyObject *args,
			      PyObject *kwargs)
{
    static char *kwlist[] = { "src", NULL };
    PyObject *py_src;
    GdkRectangle src, dest = {0, 0, 0, 0};
    
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:GdkRectangle.intersect",
				     kwlist, &py_src)) {
        return NULL;
    }
    
    if (!pyg_boxed_check(py_src, GDK_TYPE_RECTANGLE)) {
	if (!pygdk_rectangle_from_pyobject(py_src, &src)) {
	    PyErr_Clear();
	    PyErr_SetString(PyExc_TypeError,
			    "src must be a GdkRectangle or 4-tuple");
	    return NULL;
	}
    } else {
	src = *pyg_boxed_get(py_src, GdkRectangle);
    }

    gdk_rectangle_intersect(pyg_boxed_get(self, GdkRectangle), &src, &dest);
                            
    return pyg_boxed_new(GDK_TYPE_RECTANGLE, &dest, TRUE, TRUE);
}
%%
override gdk_rectangle_union kwargs
static PyObject *
_wrap_gdk_rectangle_union(PyGObject *self, PyObject *args,
			  PyObject *kwargs)
{
    static char *kwlist[] = { "src", NULL };
    PyObject *py_src;
    GdkRectangle src, dest;
    
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:GdkRectangle.union",
				     kwlist, &py_src)) {
        return NULL;
    }
    
    if (!pyg_boxed_check(py_src, GDK_TYPE_RECTANGLE)) {
	if (!pygdk_rectangle_from_pyobject(py_src, &src)) {
	    PyErr_Clear();
	    PyErr_SetString(PyExc_TypeError,
			    "src must be a GdkRectangle or 4-tuple");
	    return NULL;
	}
    } else {
	src = *pyg_boxed_get(py_src, GdkRectangle);
    }

    gdk_rectangle_union(pyg_boxed_get(self, GdkRectangle), &src, &dest);
                            
    return pyg_boxed_new(GDK_TYPE_RECTANGLE, &dest, TRUE, TRUE);
}
%%
override-slot GdkRectangle.tp_richcompare
static PyObject *
_wrap_gdk_rectangle_tp_richcompare(PyObject *self, PyObject *other, int op)
{
    PyObject *result;

    if (PyObject_TypeCheck(self, &PyGdkRectangle_Type)
        && PyObject_TypeCheck(other, &PyGdkRectangle_Type)) {
        GdkRectangle *rect1 = pyg_boxed_get(self, GdkRectangle);
        GdkRectangle *rect2 = pyg_boxed_get(other, GdkRectangle);

        switch (op) {
        case Py_EQ:
            result = (rect1->x == rect2->x && rect1->y == rect2->y
                      && rect1->width == rect2->width && rect1->height == rect2->height
                      ? Py_True : Py_False);
            break;
        case Py_NE:
            result = (rect1->x != rect2->x || rect1->y != rect2->y
                      || rect1->width != rect2->width || rect1->height != rect2->height
                      ? Py_True : Py_False);
            break;
        default:
            result = Py_NotImplemented;
        }
    }
    else
        result = Py_NotImplemented;

    Py_INCREF(result);
    return result;
}
%%
override-slot GdkRectangle.tp_repr
static PyObject *
_wrap_gdk_rectangle_tp_repr(PyGBoxed *self)
{
    GdkRectangle *rect = pyg_boxed_get(self, GdkRectangle);
    return PyString_FromFormat("%s(%d, %d, %d, %d)", self->ob_type->tp_name,
                               rect->x, rect->y, rect->width, rect->height);
}
